import { onShow, onLoad } from '@dcloudio/uni-app';
import { useThemeStore } from '@/store/modules/theme';

// 定义主题颜色接口
interface ThemeColor {
  frontColor: string;
  backgroundColor: string;
}

// 定义主题颜色映射接口
interface ThemeColors {
  [key: string]: ThemeColor;
}

export default function useThemeMixin() {
  const themeStore = useThemeStore();
  
  // 页面颜色设置函数
  const setNavigationBarTheme = () => {
    const theme = themeStore.currentTheme;
    const themeColors: ThemeColors = {
      wedding: {
        frontColor: '#ffffff',
        backgroundColor: '#FF4D4F'
      },
      funeral: {
        frontColor: '#ffffff',
        backgroundColor: '#2c3e50'
      },
      birthday: {
        frontColor: '#ffffff',
        backgroundColor: '#FF4D4F'
      },
      graduation: {
        frontColor: '#ffffff',
        backgroundColor: '#1890FF'
      },
      business: {
        frontColor: '#ffffff',
        backgroundColor: '#722ED1'
      },
      housewarming: {
        frontColor: '#ffffff',
        backgroundColor: '#52C41A'
      },
      fullmoon: {
        frontColor: '#ffffff',
        backgroundColor: '#F759AB'
      },
      longevity: {
        frontColor: '#ffffff',
        backgroundColor: '#FAAD14'
      },
      other: {
        frontColor: '#ffffff',
        backgroundColor: '#13C2C2'
      }
    };

    // 设置导航栏颜色
    uni.setNavigationBarColor({
      frontColor: themeColors[theme]?.frontColor || '#ffffff',
      backgroundColor: themeColors[theme]?.backgroundColor || '#13C2C2',
      animation: {
        duration: 0, // 设置为0防止颜色过渡动画
        timingFunc: 'linear'
      }
    });
  }
  
  // 在页面加载时立即设置导航栏颜色
  onLoad(() => {
    setNavigationBarTheme();
  });

  // 在页面显示时同步导航栏颜色，处理从其他页面返回的情况
  onShow(() => {
    setNavigationBarTheme();
  });
} 